#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
    FILE *fp = fopen("./bite", "wb+");
    if (fp == NULL) {
        perror("fopen");
        return -1; 
    }   
    fseek(fp, 0, SEEK_SET);
    char *data = "linux so easy!\n";
    size_t ret = fwrite(data, 1, strlen(data), fp);
    if (ret != strlen(data)) {
        perror("fwrite error");
        return -1; 
    }   
    fseek(fp, 0, SEEK_SET);//跳转读写位置到，从文件起始位置开始偏移0个字节
    char buf[1024] = {0};
    ret = fread(buf, 1, 1023, fp);
    if (ret == 0) {
        if (ferror(fp)) //判断上一次IO操作是否正确
            printf("fread error\n");
        if (feof(fp)) //判断是否读取到了文件末尾
            printf("read end of file!\n");
        return -1; 
    }   
    printf("%s", buf);
    fclose(fp);
    return 0;
}
// int main()
// {
//     umask(0);
//     int fd1 = open("bit.txt",O_WRONLY | O_CREAT,0644);
//     const char* msg = "Linux so easy!";
//     write(fd1,msg,strlen(msg));
    
//     int fd2 = open("bit.txt",O_RDONLY);
//     char buffer[1024] = {0};
//     ssize_t n = read(fd2,buffer,sizeof(buffer));
//     perror("read");
//     printf("n:%d\n",n);
//     if(n > 0)
//     {
//         buffer[n] = '\0';
//         printf("%s\n",buffer);
//     }
//     close(fd1);
//     close(fd2);
//     return 0;
// }